package xyz.heyaoshare.core.mapper.system;

import com.mybatisflex.core.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import xyz.heyaoshare.core.entity.system.User;

import java.util.List;
import java.util.Set;

/**
 * 用户 Mapper
 * @author YueHe
 * @version 0.0.1
 * @since 2025/6/25 上午11:47
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据用户ID查询权限
     * @param userId 用户ID
     * @return 权限列表
     */
    @Select("select distinct(authority) from sys_menu where deleted = 0 and status = 1 and authority is not null and authority != '' and id in(select distinct(menu_id) from sys_role_menu where role_id in (select role_id from sys_user_role where user_id = #{userId}))")
    Set<String> selectPermissions(Long userId);

    /**
     * 查询所有权限
     * @return 权限列表
     */
    @Select("select distinct(authority) from sys_menu where deleted = 0 and status = 1 and authority is not null and authority != ''")
    Set<String> selectPermissionsAll();

    /**
     * 根据用户ID查询角色
     * @param userId 用户ID
     * @return 角色列表
     */
    @Select("select distinct(role_code) from sys_role where deleted = 0 and status = 1 and id in (select role_id from sys_user_role where user_id = #{userId})")
    List<String> selectRoles(Long userId);

    /**
     * 查询所有角色
     * @return 角色列表
     */
    @Select("select distinct(role_code) from sys_role where deleted = 0 and status = 1")
    List<String> selectRolesAll();

}